<template>
  <div class="app-container" style="margin-top: 15px">
    <el-card class="filter-container" shadow="never">
      <div>
        <el-icon size="18" style="position: relative; top: 4px; margin: 0 5px 0 10px"><Search /></el-icon>
        <span>筛选搜索</span>
        <el-button style="float: right" @click="handleSearchList()" type="primary" size="small"> 查询结果 </el-button>
        <el-button style="float: right; margin-right: 15px" @click="handleResetSearch()" size="small"> 重置 </el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form :inline="true" :model="listQuery" size="small" label-width="140px">
          <el-form-item label="输入搜索：">
            <el-input style="width: 203px" v-model="listQuery.keyword" placeholder="商品名称"></el-input>
          </el-form-item>
          <el-form-item label="商品货号：">
            <el-input style="width: 203px" v-model="listQuery.productSn" placeholder="商品货号"></el-input>
          </el-form-item>
          <el-form-item label="商品分类：">
            <el-cascader clearable ref="ProductType" v-model="selectProductCateValue" :options="productCateOptions" placeholder="商品分类" @change="cascaderChange()"> </el-cascader>
          </el-form-item>
          <el-form-item label="商品品牌：">
            <el-select v-model="listQuery.brandId" placeholder="请选择品牌" clearable collapse-tags-tooltip>
              <el-option v-for="item in brandOptions" :key="item.value" :label="item.label" :value="item.value"> </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="上架状态：">
            <el-select v-model="listQuery.publishStatus" placeholder="全部" clearable>
              <el-option v-for="item in publishStatusOptions" :key="item.value" :label="item.label" :value="item.value"> </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="审核状态：">
            <el-select v-model="listQuery.verifyStatus" placeholder="全部" clearable>
              <el-option v-for="item in verifyStatusOptions" :key="item.value" :label="item.label" :value="item.value"> </el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <div style="margin-top: 15px">
      <el-card class="operate-container" shadow="never">
        <el-row>
          <el-col :span="4">
            <el-icon size="18" style="position: relative; top: 4px; margin: 0 5px 0 10px"><Tickets /></el-icon>
            <span>数据列表</span>
          </el-col>
          <el-col :offset="18" :span="2">
            <el-button class="btn-add" @click="handleAddProduct()" size="default"> 添加 </el-button>
          </el-col>
        </el-row>
      </el-card>
    </div>
    <div class="table-container" style="margin-top: 15px">
      <el-table ref="productTable" :data="list" style="width: 100%" @selection-change="handleSelectionChange" v-loading="listLoading" border>
        <el-table-column type="selection" width="60" align="center"></el-table-column>
        <el-table-column label="编号" width="100" align="center">
          <template #default="scope">{{ scope.row.id }}</template>
        </el-table-column>
        <el-table-column label="商品图片" width="120" align="center">
          <template #default="scope"><img style="height: 80px" :src="scope.row.pic" /></template>
        </el-table-column>
        <el-table-column label="商品名称" align="center">
          <template #default="scope">
            <p>{{ scope.row.name }}</p>
            <p>品牌：{{ scope.row.brandName }}</p>
          </template>
        </el-table-column>
        <el-table-column label="价格/货号" width="120" align="center">
          <template #default="scope">
            <p>价格：￥{{ scope.row.price }}</p>
            <p>货号：{{ scope.row.productSn }}</p>
          </template>
        </el-table-column>
        <el-table-column label="标签" width="140" align="center">
          <template #default="scope">
            <p>
              上架：
              <el-switch @change="handlePublishStatusChange(scope.$index, scope.row)" :active-value="1" :inactive-value="0" v-model="scope.row.publishStatus"> </el-switch>
            </p>
            <p>
              新品：
              <el-switch @change="handleNewStatusChange(scope.$index, scope.row)" :active-value="1" :inactive-value="0" v-model="scope.row.newStatus"> </el-switch>
            </p>
            <p>
              推荐：
              <el-switch @change="handleRecommendStatusChange(scope.$index, scope.row)" :active-value="1" :inactive-value="0" v-model="scope.row.recommandStatus"> </el-switch>
            </p>
          </template>
        </el-table-column>
        <el-table-column label="排序" width="100" align="center">
          <template #default="scope">{{ scope.row.sort }}</template>
        </el-table-column>
        <el-table-column label="SKU库存" width="100" align="center">
          <template #default="scope">
            <el-button size="default" type="primary" :icon="Edit" @click="handleShowSkuEditDialog(scope.$index, scope.row)" circle></el-button>
          </template>
        </el-table-column>
        <el-table-column label="销量" width="100" align="center">
          <template #default="scope">{{ scope.row.sale }}</template>
        </el-table-column>
        <el-table-column label="审核状态" width="100" align="center">
          <template #default="scope">
            <p>{{ verifyStatusFilter }}</p>
            <p>
              <el-button size="default" type="primary" @click="handleShowVerifyDetail(scope.$index, scope.row)">审核详情 </el-button>
            </p>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="160" align="center">
          <template #default="scope">
            <p>
              <el-button size="default" @click="handleShowProduct(scope.$index, scope.row)">查看 </el-button>
              <el-button size="default" @click="handleUpdateProduct(scope.$index, scope.row)">编辑 </el-button>
            </p>
            <p>
              <el-button size="default" @click="handleShowLog(scope.$index, scope.row)">日志 </el-button>
              <el-button size="default" type="danger" @click="handleDelete(scope.$index, scope.row)">删除 </el-button>
            </p>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container" style="margin-top: 20px">
      <el-row>
        <el-col :span="6">
          <el-select size="small" v-model="operateType" placeholder="批量操作">
            <el-option v-for="item in operates" :key="item.value" :label="item.label" :value="item.value"> </el-option>
          </el-select>
          <el-button style="margin-left: 20px" class="search-button" @click="handleBatchOperate()" type="primary" size="default"> 确定 </el-button>
        </el-col>
        <el-col :offset="10" :span="8">
          <el-config-provider :locale="locale">
            <el-pagination
              background
              small="small"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              layout="total, sizes,prev, pager, next,jumper"
              :page-size="listQuery.pageSize"
              :page-sizes="[5, 10, 15]"
              v-model:current-page="listQuery.pageNum"
              :total="total"
              prev-text="上一页"
              next-text="下一页"
            >
            </el-pagination>
          </el-config-provider>
        </el-col>
      </el-row>
    </div>
    <el-dialog title="编辑货品信息" v-model="editSkuInfo.dialogVisible" width="40%">
      <span>商品货号：</span>
      <span>{{ editSkuInfo.productSn }}</span>
      <el-input placeholder="按sku编号搜索" v-model="editSkuInfo.keyword" size="small" style="width: 50%; margin-left: 20px">
        <el-button size="default" icon="el-icon-search" @click="handleSearchEditSku"></el-button>
      </el-input>
      <el-table style="width: 100%; margin-top: 20px" :data="editSkuInfo.stockList" border>
        <el-table-column label="SKU编号" align="center">
          <template #default="scope">
            <el-input v-model="scope.row.skuCode"></el-input>
          </template>
        </el-table-column>
        <el-table-column v-for="(item, index) in editSkuInfo.productAttr" :label="item.name" :key="item.id" align="center">
          <template #default="scope">
            {{ getProductSkuSp(scope.row, index) }}
          </template>
        </el-table-column>
        <el-table-column label="销售价格" width="80" align="center">
          <template #default="scope">
            <el-input v-model="scope.row.price"></el-input>
          </template>
        </el-table-column>
        <el-table-column label="商品库存" width="80" align="center">
          <template #default="scope">
            <el-input v-model="scope.row.stock"></el-input>
          </template>
        </el-table-column>
        <el-table-column label="库存预警值" width="100" align="center">
          <template #default="scope">
            <el-input v-model="scope.row.lowStock"></el-input>
          </template>
        </el-table-column>
      </el-table>
      <div class="dialog-footer" style="margin-top: 10px">
        <el-button size="default" @click="editSkuInfo.dialogVisible = false">取 消</el-button>
        <el-button size="default" type="primary" @click="handleEditSkuConfirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script setup>
import { fetchList, updateDeleteStatusApi, updateNewStatusApi, updateRecommendStatusApi, updatePublishStatusApi } from '@/plugins/product'
import { fetchList as fetchSkuStockList, update as updateSkuStockList } from '@/plugins/skuStock'
import { fetchList as fetchProductAttrList } from '@/plugins/productAttr'
import { fetchList as fetchBrandList } from '@/plugins/brand'
import { fetchListWithChildren } from '@/plugins/productCate'
import { reactive, onBeforeMount, computed, ref, getCurrentInstance } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useRouter } from 'vue-router'
import { Edit, Tickets, Search } from '@element-plus/icons-vue'
import zhCn from 'element-plus/dist/locale/zh-cn.mjs'

const router = useRouter()

const locale = zhCn

const { proxy } = getCurrentInstance()

let defaultListQuery = reactive({
  keyword: '',
  pageNum: 1,
  pageSize: 5,
  publishStatus: null,
  verifyStatus: null,
  productSn: null,
  productCategoryId: null,
  brandId: ''
})

const editSkuInfo = reactive({
  dialogVisible: false,
  productId: null,
  productSn: '',
  productAttributeCategoryId: null,
  stockList: [],
  productAttr: [],
  keyword: null
})

const operates = reactive([
  {
    label: '商品上架',
    value: 'publishOn'
  },
  {
    label: '商品下架',
    value: 'publishOff'
  },
  {
    label: '设为推荐',
    value: 'recommendOn'
  },
  {
    label: '取消推荐',
    value: 'recommendOff'
  },
  {
    label: '设为新品',
    value: 'newOn'
  },
  {
    label: '取消新品',
    value: 'newOff'
  },
  {
    label: '转移到分类',
    value: 'transferCategory'
  },
  {
    label: '移入回收站',
    value: 'recycle'
  }
])

let operateType = ref('')
let listQuery = reactive({
  keyword: null,
  pageNum: 1,
  pageSize: 5,
  publishStatus: null,
  verifyStatus: null,
  productSn: null,
  productCategoryId: null,
  brandId: null
})
let list = reactive([])
let total = ref(0)
let listLoading = ref(true)

let selectProductCateValue = reactive([])
let multipleSelection = reactive([])

let productCateOptions = reactive([])

let brandOptions = reactive([])
const publishStatusOptions = reactive([
  {
    value: 1,
    label: '上架'
  },
  {
    value: 0,
    label: '下架'
  }
])

const verifyStatusOptions = reactive([
  {
    value: 1,
    label: '审核通过'
  },
  {
    value: 0,
    label: '未审核'
  }
])

onBeforeMount(() => {
  getList()
  getBrandList()
  getProductCateList()
})

function cascaderChange() {
  listQuery.productCategoryId = proxy.$refs['ProductType'].getCheckedNodes()[0].value
}

const verifyStatusFilter = computed(() => {
  if (defaultListQuery.verifyStatus === 1) {
    return '审核通过'
  } else {
    return '未审核'
  }
})

function getProductSkuSp(row, index) {
  let spData = JSON.parse(row.spData)
  if (spData != null && index < spData.length) {
    return spData[index].value
  } else {
    return null
  }
}

function getList() {
  listLoading.value = true
  fetchList(listQuery).then(response => {
    listLoading.value = false
    list = response.data.list
    total.value = response.data.total
  })
}

function getBrandList() {
  fetchBrandList({ pageNum: 1, pageSize: 100 }).then(response => {
    let brandList = []
    if (response.data.list != null && response.data.list) {
      brandList = response.data.list
      for (let i = 0; i < response.data.list.length; i++) {
        brandOptions.push({ label: brandList[i].name, value: brandList[i].id })
      }
    }
  })
}

function getProductCateList() {
  fetchListWithChildren().then(response => {
    let list = response.data
    for (let i = 0; i < list.length; i++) {
      let children = []
      if (list[i].children != null && list[i].children.length > 0) {
        for (let j = 0; j < list[i].children.length; j++) {
          children.push({ label: list[i].children[j].name, value: list[i].children[j].id })
        }
      }
      productCateOptions.push({ label: list[i].name, value: list[i].id, children: children })
    }
  })
}

function handleShowSkuEditDialog(index, row) {
  editSkuInfo.dialogVisible = true
  editSkuInfo.productId = row.id
  editSkuInfo.productSn = row.productSn
  editSkuInfo.productAttributeCategoryId = row.productAttributeCategoryId
  editSkuInfo.keyword = null
  fetchSkuStockList(row.id, { keyword: editSkuInfo.keyword }).then(response => {
    editSkuInfo.stockList = response.data
  })
  if (row.productAttributeCategoryId != null) {
    fetchProductAttrList(row.productAttributeCategoryId, { type: 0 }).then(response => {
      editSkuInfo.productAttr = response.data.list
    })
  }
}

function handleSearchEditSku() {
  fetchSkuStockList(editSkuInfo.productId, { keyword: editSkuInfo.keyword }).then(response => {
    editSkuInfo.stockList = response.data
  })
}
function handleEditSkuConfirm() {
  if (editSkuInfo.stockList == null || editSkuInfo.stockList.length <= 0) {
    ElMessage({
      message: '暂无sku信息',
      type: 'warning',
      showClose: true,
      duration: 1000
    })
    return
  }
  ElMessageBox.confirm('是否要进行修改', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    updateSkuStockList(editSkuInfo.productId, editSkuInfo.stockList).then(() => {
      ElMessage({
        message: '修改成功',
        type: 'success',
        showClose: true,
        duration: 1000
      })
      editSkuInfo.dialogVisible = false
    })
  })
}

function handleSearchList() {
  listQuery.pageNum = 1
  getList()
}
function handleAddProduct() {
  router.push('/addProduct')
}

function handleBatchOperate() {
  if (operateType.value === '') {
    ElMessage({
      message: '请选择操作类型',
      type: 'warning',
      duration: 1000
    })
    return
  }
  if (multipleSelection == null || multipleSelection.length < 1) {
    ElMessage({
      message: '请选择要操作的商品',
      type: 'warning',
      duration: 1000
    })
    return
  }
  ElMessageBox.confirm('是否要进行该批量操作?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    let ids = []
    for (let i = 0; i < multipleSelection.length; i++) {
      ids.push(multipleSelection[i].id)
    }
    switch (operateType.value) {
      case operates[0].value:
        updatePublishStatus(1, ids)
        break
      case operates[1].value:
        updatePublishStatus(0, ids)
        break
      case operates[2].value:
        updateRecommendStatus(1, ids)
        break
      case operates[3].value:
        updateRecommendStatus(0, ids)
        break
      case operates[4].value:
        updateNewStatus(1, ids)
        break
      case operates[5].value:
        updateNewStatus(0, ids)
        break
      case operates[6].value:
        break
      case operates[7].value:
        updateDeleteStatus(1, ids)
        break
      default:
        break
    }
  })
}

function handleSizeChange(val) {
  listQuery.pageNum = 1
  listQuery.pageSize = val
  getList()
}
function handleCurrentChange(val) {
  listQuery.pageNum = val
  getList()
}
function handleSelectionChange(val) {
  multipleSelection = val
}
function handlePublishStatusChange(index, row) {
  let ids = []
  ids.push(row.id)
  updatePublishStatus(row.publishStatus, ids)
}
function handleNewStatusChange(index, row) {
  let ids = []
  ids.push(row.id)
  updateNewStatus(row.newStatus, ids)
}

function handleRecommendStatusChange(index, row) {
  let ids = []
  ids.push(row.id)
  updateRecommendStatus(row.recommandStatus, ids)
}
function handleResetSearch() {
  selectProductCateValue.value = reactive([])
  listQuery.keyword = ''
  listQuery.pageNum = 1
  listQuery.pageSize = 5
  listQuery.publishStatus = null
  listQuery.verifyStatus = null
  listQuery.productSn = null
  listQuery.productCategoryId = null
  listQuery.brandId = null

  defaultListQuery.keyword = ''
  defaultListQuery.pageNum = 1
  defaultListQuery.pageSize = 5
  defaultListQuery.publishStatus = null
  defaultListQuery.verifyStatus = null
  defaultListQuery.productSn = null
  defaultListQuery.productCategoryId = null

  operateType.value = ''
}

function handleDelete(index, row) {
  ElMessageBox.confirm('是否要进行删除操作?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    let ids = []
    ids.push(row.id)
    updateDeleteStatus(1, ids)
  })
}
function handleUpdateProduct(index, row) {
  router.push({ path: '/updateProduct', query: { id: row.id } })
}

function handleShowProduct(index, row) {
  console.log('handleShowProduct', row)
}
function handleShowVerifyDetail(index, row) {
  console.log('handleShowVerifyDetail', row)
}
function handleShowLog(index, row) {
  console.log('handleShowLog', row)
}

function updatePublishStatus(publishStatus, ids) {
  let params = new URLSearchParams()
  params.append('ids', ids)
  params.append('publishStatus', publishStatus)
  updatePublishStatusApi(params).then(() => {
    ElMessage({
      message: '修改成功',
      type: 'success',
      duration: 1000
    })
    getList()
  })
}

function updateNewStatus(newStatus, ids) {
  let params = new URLSearchParams()
  params.append('ids', ids)
  params.append('newStatus', newStatus)
  updateNewStatusApi(params).then(() => {
    ElMessage({
      message: '修改成功',
      type: 'success',
      duration: 1000
    })
    getList()
  })
}

function updateRecommendStatus(recommendStatus, ids) {
  let params = new URLSearchParams()
  params.append('ids', ids)
  params.append('recommendStatus', recommendStatus)
  updateRecommendStatusApi(params).then(() => {
    ElMessage({
      message: '修改成功',
      type: 'success',
      duration: 1000
    })
    getList()
  })
}

function updateDeleteStatus(deleteStatus, ids) {
  let params = new URLSearchParams()
  params.append('ids', ids)
  params.append('deleteStatus', deleteStatus)
  updateDeleteStatusApi(params).then(() => {
    ElMessage({
      message: '删除成功',
      type: 'success',
      duration: 1000
    })
    getList()
  })
}
</script>
<style></style>
